package org.dfl.core.model.transitions;

import java.util.ArrayList;
import java.util.Collection;

import org.dfl.core.data.connectors.CollectionTypeConnector;
import org.dfl.core.data.connectors.TypeConnector;
import org.dfl.core.data.types.Type;
import org.dfl.core.data.types.TypeManager;
import org.dfl.core.model.TransitionExecutionException;
import org.dfl.messages.Messages;

/**
 * transition that flatten collections
 * @author Piotr Wlodarczyk
 *
 */
public class FlattenTransition extends OneInputTransition {
	@SuppressWarnings("unchecked") //$NON-NLS-1$
	@Override
	protected Object fireSingle(Object input) throws TransitionExecutionException {
		Collection<Collection> inCol = (Collection<Collection>) input;
		Collection res = new ArrayList();
		for(Collection c: inCol){
			res.addAll(c);
		}
		return res;
	}

	@Override
	protected Type getDefaultInputDefinition() {
		return TypeManager.getCollectionType(TypeManager.getCollectionType());
	}

	@Override
	protected Type getDefaultOutputDefinition() {
		return TypeManager.getCollectionType();
	}
	

	@Override
	protected void connectInputAndOutput() {
		TypeConnector connector = new CollectionTypeConnector(getOutputTypeContainer(), getInputContainer());
		getInputContainer().connectBy(connector);
	}


	@Override
	public String getDefaultDescription() {
		return Messages.FlattenTransition_description;
	}

}
